package cn.xxd.xxdwebportal.controller;

import cn.xxd.entity.HomeContent;
import cn.xxd.entity.LoginUser;
import cn.xxd.entity.ShopUser;
import cn.xxd.entity.XxdItem;
import cn.xxd.service.ContentService;
import cn.xxd.xxdssointerface.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class IndexController {

    @Reference(version = "1.0.0", timeout = 100000)
    private ContentService contentService;

    @Reference(version = "1.0.0", timeout = 100000)
    private UserService userService;

    @Value("${CAERTURL}")
    private String CAERTURL;

    @Value("${SSOURL}")
    private String SSOURL;

    @Value("${SEARCHURL}")
    private String SEARCHURL;

    @Value("${HOMEURL}")
    private String HOMEURL;

    /**
     * 生产者的组名
     */
    @Value("${suning.rocketmq.producerGroup}")
    private String producerGroup;

    /**
     * NameServer 地址
     */
    @Value("${suning.rocketmq.namesrvaddr}")
    private String namesrvAddr;

//    @RequestMapping("/")
//    public String index(){
//        return "index";
//    }

    @Value("${suning.rocketmq.topic.visit}")
    private String TOPIC;

    @Value("${suning.rocketmq.tags.visit}")
    private String TAGS;

    @RequestMapping("/login")
    public String login() {
        return "login";
    }

    @RequestMapping("/")
    public String findHomePic(HttpServletRequest request, Model model) {
        LoginUser user = (LoginUser) request.getAttribute("user");
        ShopUser shupUser = userService.getShopUserByUserId(user.getId() + "");
        user.setName(shupUser.getName()); // 改bug 2020/02/21
        //查询购物车
        List<XxdItem> homeCartItems = null;
        //轮播图
        List<HomeContent> homeContents = null;
        //本月推荐
        List<XxdItem> activitieItems = null;
        //本月特价
        List<XxdItem> specialItems = null;
        //新品上市
        List<XxdItem> newItems = null;
        //最佳销售
        List<XxdItem> bestItems = null;

        try {
            homeCartItems = contentService.findHomeCartItem((int) user.getId());
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("查询购物车出现异常");
        }

        try {
            homeContents = contentService.find();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("查询轮播图出现异常");
        }

        try {
            activitieItems = contentService.findXxdActivitieItem(0);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("查询本月推荐出现异常");
        }

        try {
            specialItems = contentService.findXxdActivitieItem(1);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("查询本月特价出现异常");
        }

        try {
            newItems = contentService.findXxdActivitieItem(2);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("查询新品上市出现异常");
        }

        try {
            bestItems = contentService.findXxdActivitieItem(3);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("查询新品上市出现异常");
        }


        model.addAttribute("homeCartItems", homeCartItems);
        model.addAttribute("homeContent", homeContents);
        model.addAttribute("activitieItems", activitieItems);
        model.addAttribute("specialItems", specialItems);
        model.addAttribute("newItems", newItems);
        model.addAttribute("bestItems", bestItems);
        model.addAttribute("user", user);
        model.addAttribute("CAERTURL", CAERTURL);
        model.addAttribute("SSOURL", SSOURL);
        model.addAttribute("SEARCHURL", SEARCHURL);
        model.addAttribute("HOMEURL", HOMEURL);

        // 这里用mq统计网站访问次数
        DefaultMQProducer producer = new DefaultMQProducer(producerGroup);
        producer.setNamesrvAddr(namesrvAddr);
        try {
            producer.start();
            System.out.println("发送消息:"+ TAGS);
            Message message = new Message(TOPIC, TAGS, TAGS.getBytes(RemotingHelper.DEFAULT_CHARSET));
            SendResult result = producer.send(message);
            System.err.println("发送响应：MsgId:" + result.getMsgId() + "，发送状态:" + result.getSendStatus());
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("mq发送消息失败");
        } finally {
            producer.shutdown();
        }
        return "index";
    }

    @RequestMapping("/sort")
    public String sort(String name, HttpServletRequest request, Model model) {
        LoginUser user = (LoginUser) request.getAttribute("user");
        ShopUser shupUser = userService.getShopUserByUserId(user.getId() + "");
        user.setName(shupUser.getName());
        //查询购物车
        List<XxdItem> homeCartItems = null;
        //轮播图
        List<HomeContent> homeContents = null;
        //本月推荐
        List<XxdItem> activitieItems = null;
        //本月特价
        List<XxdItem> specialItems = null;
        //新品上市
        List<XxdItem> newItems = null;
        //最佳销售
        List<XxdItem> bestItems = null;

        if ("本月推荐".equals(name)) {
            try {
                activitieItems = contentService.findXxdActivitieItem(0);
                model.addAttribute("items", activitieItems);
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("查询本月推荐出现异常");
            }
        } else if ("本月特价".equals(name)) {
            try {
                specialItems = contentService.findXxdActivitieItem(1);
                model.addAttribute("items", specialItems);
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("查询本月特价出现异常");
            }
        } else if ("新品上市".equals(name)) {
            try {
                newItems = contentService.findXxdActivitieItem(2);
                model.addAttribute("items", newItems);
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("查询新品上市出现异常");
            }
        } else if ("最佳销售".equals(name)) {
            bestItems = contentService.findXxdActivitieItem(3);
            model.addAttribute("items", bestItems);
        } else {
            System.out.println("系统出现异常");
            System.out.println("查询最佳销售出现异常");
        }

        try {
            homeCartItems = contentService.findHomeCartItem((int) user.getId());
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("查询购物车出现异常");
        }

        try {
            homeContents = contentService.find();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("查询轮播图出现异常");
        }

        model.addAttribute("homeCartItems", homeCartItems);
        model.addAttribute("user", user);
        model.addAttribute("CAERTURL", CAERTURL);
        model.addAttribute("SSOURL", SSOURL);
        model.addAttribute("SEARCHURL", SEARCHURL);
        model.addAttribute("HOMEURL", HOMEURL);
        model.addAttribute("homeContent", homeContents);
        model.addAttribute("name", name);
        return "sort";
    }

}
